<?php

class Init {
	private static $_instance;
	// Do not allow an explicit call of the constructor: $v = new Singleton();
	final private function __construct() {}
	// Do not allow the clone operation: $x = clone $v;
	final private function __clone() {}

	public static function getInstance() {
		if (!(self::$_instance instanceof self)) {
			self::$_instance = new self();
			self::setSessionAutoStart();
			self::showErrors();
			self::setEncoding();
			self::gzip();
			self::setCache();
			self::setDate();
		}
		return self::$_instance;
	}
	final private static function setSessionAutoStart(){
		ini_set('session.auto_start', '0');
	}
	final private static function gzip() {
		ini_set('zlib.output_compression', true);
		ini_set('zlib.output_compression_level', 9);
		ini_set('output_buffering', true);
		ob_start("zlib.output_compression");
		ob_implicit_flush(1);
	}

	final private static function showErrors() {
		error_reporting(E_ALL | E_STRICT);
		ini_set("display_errors", 1);
	}
	final public static function smarty() {

		$path=$_SERVER['DOCUMENT_ROOT'] . '/../../' . '/etc/lib/Smarty-3.0.7/libs/';
		set_include_path(get_include_path() . PATH_SEPARATOR . $path);
		require_once('Smarty.class.php');
		$smarty = new Smarty();
		$smarty->debugging = false;
		$smarty->caching = false;
		$smarty->force_compile = true;
		$smarty->compile_check=true;
		$smarty->security = true;

		//diferentes para cada projecto
		$smarty->compile_dir = getcwd() . '/etc/view_c';
		$smarty->cache_dir = getcwd() . '/etc/cache';

		//Vista diferente para cada classe
		$smarty->template_dir = getcwd() . '/view';
		if (!is_dir(getcwd() . '/view')) {
			mkdir(getcwd() . '/view',0777);
			//echo 'Create template dir in ' . getcwd() . '/view';
		}
		return $smarty;
	}
	final public static function session() {
		global $dev;
		if ($dev) {
			session_cache_limiter("nocache");
		} else {
			session_cache_limiter("public");
		}
		session_start();
	}

	final private static function setEncoding() {
		header('Content-type: text/html; charset=utf-8');
		/* Set UTF-8 settings */
		// O php da money ainda é o  5.2.6 não tem o mb_
		mb_internal_encoding('UTF-8');
		mb_regex_encoding('UTF-8');
		mb_http_output('UTF-8');
		//$s=mb_detect_encoding($s, "UTF-8") == "UTF-8" ? $s : $s = utf8_encode($s);
		//echo 'init';die;
		/* Set ICONV ini settings */
		ini_set('iconv.input_encoding', 'UTF-8');
		ini_set('iconv.output_encoding', 'UTF-8');
		ini_set('iconv.internal_encoding', 'UTF-8');
		/* Set ICONV function settings */
		iconv_set_encoding('input_encoding', 'UTF-8');
		iconv_set_encoding('output_encoding', 'UTF-8');
		iconv_set_encoding('internal_encoding', 'UTF-8');
	}
	final public static function setCache() {
		global $dev;
		$offset = 60 * 60 * 24 * 30; /* 30 dias */
		if ($dev===true) {
			$expire = "Expires: " . gmdate("D, d M Y H:i:s", time() - $offset) . " GMT";
			header($expire);
			header('Cache-Control: max-age=-'.$offset);
			session_cache_limiter('nocache');
			header('Pragma: no-cache');
		} else {
			$expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
			header($expire);
			header('Cache-Control: public');
			header('Cache-Control: max-age='.$offset);
			header('Pragma: public');
			session_cache_limiter('public');
		}
	}
	final private static function setDate() {
		date_default_timezone_set('Europe/Lisbon');
	}
	final public static function setMemoryLimit() {
		//echo 'memory_limit';
		ini_set('memory_limit', -1); //sem limite
		//ini_set('memory_limit', '512M'); //sem limite
		//echo memory_get_usage() . "\n";
	}
	final public static function setTimeLimit() {
		set_time_limit(0);
		ini_set('max_execution_time',0);
		ini_set('safe_mode',0);
		//ini_set('memory_limit', -1); //sem limite
	}
}
